package life.mahu.community.controller;

import com.github.pagehelper.PageInfo;
import life.mahu.community.dto.PageResultDTO;
import life.mahu.community.dto.ResultDTO;
import life.mahu.community.model.Question;
import life.mahu.community.model.SensitiveWords;
import life.mahu.community.util.SensitiveWordFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@Slf4j
public class AdminSensitiveController {

    private SensitiveWordFilter sensitiveWordFilter;

    @Autowired
    public AdminSensitiveController(SensitiveWordFilter sensitiveWordFilter) {
        this.sensitiveWordFilter = sensitiveWordFilter;
    }

    @GetMapping("/admin/sensitive/list")
    public String getListHtml(){
        return "/admin/sensitive/list";
    }

    @GetMapping("/admin/sensitive/add")
    public String getAddHtml(){
        return "/admin/sensitive/add";
    }

    @ResponseBody
    @GetMapping("/admin/sensitive/getList")
    public PageResultDTO getSensitiveList(@RequestParam("keyword") String keyword,@RequestParam("limit") Integer limit,
                                          @RequestParam("page") Integer page){
        //List<User> userList = userService.getUserList(user);
        PageInfo sensitiveListPage = sensitiveWordFilter.getSensitiveWordsPage(keyword,page,limit);
        return PageResultDTO.okOf(0, "查询成功！", sensitiveListPage.getTotal(), sensitiveListPage.getList());
    }

    @ResponseBody
    @DeleteMapping("/admin/sensitive")
    public ResultDTO delSensitive(@RequestParam("delWord") String delWord){
        //从服务端查询数据
        Boolean success = sensitiveWordFilter.delWord(delWord);
        if(success){
            return ResultDTO.okOf(200, "删除成功！");
        }else{
            return ResultDTO.okOf(201, "查无数据！");
        }
    }

    @ResponseBody
    @PostMapping("/admin/sensitive")
    public ResultDTO saveSensitive(HttpServletRequest request, HttpServletResponse response){

        ResultDTO resultDTO = new ResultDTO();
        try {
            Map<String, String[]> map = request.getParameterMap();
            SensitiveWords sensitiveWords=new SensitiveWords();
            BeanUtils.populate(sensitiveWords, map);
            Boolean success = sensitiveWordFilter.addWord(sensitiveWords.getWord());
            if(success){
                resultDTO.setCode(200);
                resultDTO.setMessage("保存成功！");
            }else{
                resultDTO.setCode(100);
                resultDTO.setMessage("系统繁忙！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultDTO.setCode(101);
            resultDTO.setMessage("服务器出错了！");
        }

        return resultDTO;

    }
}
